package com.yan;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.core.MessagePropertiesBuilder;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.support.ClassPathXmlApplicationContext;


/**
 * @author WangYan
 * @description:
 * @date 2021/7/5 19:20
 */
public class ProducerApp {
    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-rabbit.xml");
        RabbitTemplate template = context.getBean(RabbitTemplate.class);

        MessagePropertiesBuilder propertiesBuilder = MessagePropertiesBuilder.newInstance();
        propertiesBuilder.setContentEncoding("gbk");
        propertiesBuilder.setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN);


        for (int i = 0; i < 100; i++) {
            Message msg = MessageBuilder.withBody(("你好 世界  "+i).getBytes("gbk"))
                    .andProperties(propertiesBuilder.build()).build();

            template.send("ex.direct.name.yan", "routing.q1.name.yan", msg);
        }

        context.close();
    }
}
